package com.linhh.mall.center.web.controller;

import com.linhh.mall.center.api.MenuInfoService;
import com.linhh.mall.center.api.RoleInfoService;
import com.linhh.mall.center.api.UserInfoService;
import com.linhh.mall.center.common.pojo.MenuInfo;
import com.linhh.mall.center.common.pojo.MenuVo;
import com.linhh.mall.center.common.pojo.UserInfo;
import com.linhh.mall.center.common.util.HttpResult;
import com.linhh.mall.center.common.util.SecurityUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 菜单信息控制类
 * @Author linhh
 * @Date 2022 10 22 11 50
 **/
@RestController
@RequestMapping("/api/menus")
public class MenuInfoController {

    @Reference
    private UserInfoService userInfoService;

    @Reference
    private RoleInfoService roleInfoService;

    @Reference
    private MenuInfoService menuInfoService;

    /**
     *
     * @return
     */
    @GetMapping(value="/build")
    @ResponseBody
    public HttpResult buildMenus(){
        UserInfo userInfo = userInfoService.findByUserName(SecurityUtils.getUserName());
        System.out.println("输出用户ID:"+userInfo.getId());
        List<MenuInfo> menuInfoList = menuInfoService.findByRoles(roleInfoService.selectRoleByUserId(userInfo.getId()));
        List<MenuInfo> menuInfos = (List<MenuInfo>) menuInfoService.buildTree(menuInfoList).get("content");
        List<MenuVo>  menuVos = menuInfoService.buildMenus(menuInfos);
        return HttpResult.success(menuVos);
    }
}
